package com.runfast.waimai.web.controller.Driver_controller;

import com.runfast.paotui.dao.model.RunfastLoginRecord;
import com.runfast.paotui.dao.model.RunfastShopper;
import com.runfast.paotui.service.RunfastLoginRecordService;
import com.runfast.paotui.service.RunfastShopperService;


import com.runfast.waimai.util.Constant;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import sun.awt.SunHints;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;


@Controller
@RequestMapping(value = "/driver/install/*")
public class DriverInstallController extends DriverBaseController {
	//修改密码 姓名，手机号码 退出
    @Resource
    private RunfastShopperService shopperService;
	@Resource
	RunfastLoginRecordService runfastLoginRecordService;
	@Resource
	RedisTemplate redisTemplate;
	
	@RequestMapping(value="index",method=RequestMethod.POST)//验证
	public @ResponseBody ModelMap index(String token){
		ModelMap model = new ModelMap();
		RunfastShopper shopper = super.getLoginShopper(token);
		if(shopper!=null){
			RunfastShopper sh = shopperService.get(shopper.getId());
			if(sh!=null){
				model.put("shopper", sh);
			}
		}	
		return model;
 }

	//修改密码
	@RequestMapping(value="updatepassword",method=RequestMethod.POST)//验证
	public @ResponseBody ModelMap updatepassword(@RequestParam("password") String password, @RequestParam("newpassword") String newpassword,
												 @RequestParam("aginnewpassword") String aginnewpassword,String token){
	/*	System.out.println( password+"原始密码"
		  + newpassword+"新密码"+
		aginnewpassword);*/

		ModelMap model = new ModelMap();
		RunfastShopper shopper1 = super.getLoginShopper(token);
		RunfastShopper shopper=runfastShopperService.get(shopper1.getId());

		/*	 System.out.println("ddddddddddddddddddddddddddddddddddddddd"+shopper);*/
			 if (shopper != null && password != null && newpassword != null && aginnewpassword != null) {
		/*		  shopper.getPassword().equals(DigestUtils.md5Hex(password))*/
				 if (shopper.getPassword().equalsIgnoreCase(password)) {
					 if (newpassword.equals(aginnewpassword)) {
						 shopper.setPassword(newpassword);//设置加密密码
						 shopperService.update(shopper);
						 model.put("success", true);
						 model.put("msg", "修改成功");
					 } else {
						 model.put("success", false);
						 model.put("msg", "修改的密码两次输入不一致");
					 }

				 } else {
					 model.put("msg", "原密码不正确！");
					 model.put("success", false);
				 }
			 } else {
				 model.put("msg", "输入为空值！");
				 model.put("success", false);
			 }


	       	return model;
	}
	//修改手机号
	@RequestMapping(value="upmunber",method=RequestMethod.POST)//验证
	public @ResponseBody ModelMap upmunber(@RequestParam(value = "munber") String munber,String token,String number){
		ModelMap model = new ModelMap();
     RunfastShopper shopper   = super.getLoginShopper(token);
     System.out.println(munber+"=========="+number+"--------------"+shopper.getMunber());

     	if(number!=null){
			if(!number.equals(shopper.getMunber())){
				model.put("errormsg","您已在另一台手机修改了，请重新登录");
				model.put("success",false);
				return model;
			}
     	}
     if(shopper!=null){
    	 RunfastShopper sho    = shopperService.get(shopper.getId());
    	 if(munber!=null){
    		 sho.setMunber(munber);
    		 //这里可以优化，update 表名 set number=#{munber} where id=#{id}
			 sho.setUserName(munber);
			 shopperService.updateNumber(sho);
    	//	 shopperService.updateShopper(sho);
    		 model.put("msg","修改成功");
    		 model.put("success",true);
    	 }else {
			 model.put("msg","修改不成功");
			 model.put("success",false);
		 }
     }
		
		return model;
}
	////修改姓名
	@RequestMapping(value="upname",method=RequestMethod.POST)//验证
	public @ResponseBody ModelMap upname(@RequestParam(value = "name") String name,String token){
		ModelMap model = new ModelMap();
     RunfastShopper shopper   = super.getLoginShopper(token);
     if(shopper!=null){
    	 RunfastShopper sho    = shopperService.get(shopper.getId());
    	 if(name!=null){
    		 sho.setName(name);
    		 shopperService.update(sho);
    		 model.put("succ","修改成功！");
    	 }
     }
	          return model;
	}

	//退出
	@RequestMapping(value = "logout", method = RequestMethod.POST)
	public  @ResponseBody ModelMap logout(HttpServletRequest request, HttpServletResponse response,String token) {
		    ModelMap model = new ModelMap();
		RunfastShopper shopper=super.getLoginShopper(token);

		shopper.setAlias(null);
		//shopperService.update(shopper);
		shopperService.updateByPrimaryKey(shopper);

		List<RunfastLoginRecord> runfastLoginRecord=runfastLoginRecordService.getshopperLoginmsg(shopper.getId());
		for(RunfastLoginRecord s:runfastLoginRecord){
			s.setLogouttime(new Date());
			s.setAlias(null);
			runfastLoginRecordService.updateByPrimaryKeySelective(s);
		}

		/*if(shopper!=null && redisTemplate.opsForValue().get(shopper.getToken())!=null){
			redisTemplate.delete(shopper.getToken());
		}*/
		model.put("succ","已退出！");
		model.put("success",true);

		return model;
	}

}
